package com.onetech.adrewards.free.dialogs;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.google.analytics.tracking.android.Fields;
import com.google.analytics.tracking.android.MapBuilder;
import com.google.analytics.tracking.android.Tracker;
import com.onetech.adrewards.free.R;
import com.onetech.adrewards.free.activities.BaseNewActivity;
import com.onetech.adrewards.free.fragments.ImageGuideFragment;
import com.onetech.adrewards.free.fragments.ImageGuideFragment2;
import com.viewpagerindicator.CirclePageIndicator;

public class GuideDialogFragment extends DialogFragment implements OnClickListener, OnPageChangeListener {
	private View mExitBtn=null;
	private ImageView mSlideLeftBtn=null;
	private ImageView mSlideRightBtn=null;
	private View mView=null;
	private ViewPager mViewPager=null;
	//private int currentIndex=0;
	private CirclePageIndicator mPagerIndicator=null;
	
	private Tracker mTracker;
	private Context mContext=null;
	
	public interface DoAction{
		public void DissmissDialog();
	}
	private DoAction doAction = null;
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.user_guide_layout, container);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0));
        
        doAction=new DoAction() {
    		
    		@Override
    		public void DissmissDialog() {
    			try {
					dismiss();
					((BaseNewActivity) getActivity()).onNavigationDrawerItemSelected(1);
				} catch (Exception e) {					
				}
    		}
    	};
    	
        mViewPager = (ViewPager) mView.findViewById(R.id.pager);
        List<Integer> fragments = getFragments();
        MyFragmentAdapter ama = new MyFragmentAdapter(getChildFragmentManager(), fragments);
        mViewPager.setAdapter(ama);
        mPagerIndicator=(CirclePageIndicator)mView.findViewById(R.id.indicator);
        mPagerIndicator.setViewPager(mViewPager);        
        mPagerIndicator.setFillColor(Color.BLUE);
        mPagerIndicator.setVisibility(View.GONE);
        
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        
        mSlideLeftBtn=(ImageView)mView.findViewById(R.id.slideLeft);
        mSlideRightBtn=(ImageView)mView.findViewById(R.id.slideRight);
        
        mPagerIndicator.setOnPageChangeListener(this);        
        mSlideLeftBtn.setOnClickListener(this);
        mSlideRightBtn.setOnClickListener(this);
        
        mExitBtn=(LinearLayout)mView.findViewById(R.id.exitDialogBtn);
        mExitBtn.setOnClickListener(this);
        checkSlideMenuStatus();
        return mView;
    }
	private void checkSlideMenuStatus(){
		if(mViewPager.getCurrentItem()==0){
			mSlideLeftBtn.setVisibility(View.INVISIBLE);
			return;
		}
		if(mViewPager.getCurrentItem()==4){
			mSlideRightBtn.setVisibility(View.INVISIBLE);
			return;
		}
		mSlideLeftBtn.setVisibility(View.VISIBLE);
		mSlideRightBtn.setVisibility(View.VISIBLE);
	}
	@Override
	public void onClick(View v) {
		int id=v.getId();
		switch (id) {
		case R.id.exitDialogBtn:
			GuideDialogFragment.this.dismiss();
			break;
		case R.id.slideLeft:
			int curIndex=mViewPager.getCurrentItem()-1;
			if(curIndex>=0 && curIndex<5){
				mPagerIndicator.setCurrentItem(curIndex);
			}			
			break;
		case R.id.slideRight:
			int curIndex2=mViewPager.getCurrentItem()+1;
			if(curIndex2>=0 && curIndex2<5){
				mPagerIndicator.setCurrentItem(curIndex2);
			}
			break;
		default:
			break;
		}
		checkSlideMenuStatus();
	}
	@Override
	public void onPageScrollStateChanged(int arg0) {
		
	}
	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		
	}
	@Override
	public void onPageSelected(int index) {
		mPagerIndicator.setCurrentItem(index);
		checkSlideMenuStatus();
	}	
    private List<Integer> getFragments(){
        List<Integer> fList = new ArrayList<Integer>();
        	fList.add(R.drawable.help_0_4);
        	fList.add(R.drawable.help_1_4);
            fList.add(R.drawable.help_2_4);
            fList.add(R.drawable.help_3_4);
            fList.add(R.drawable.help_4_4);
        return fList;
    }
    public class MyFragmentAdapter extends FragmentPagerAdapter{

        private List<Integer> fragments;

        public MyFragmentAdapter(FragmentManager fm, List<Integer> fragments){
            super(fm);
            this.fragments = fragments;
        }

        @Override
        public Fragment getItem(int position) {
        	switch (position) {
			case 4:
				return ImageGuideFragment2.newInstance("",fragments.get(position),doAction);
			default:
				return ImageGuideFragment.newInstance("",fragments.get(position));
			}            
        }

        @Override
        public int getCount() {
            return this.fragments.size();
        }
    }	
    @Override
    public void onStart() {
        super.onStart();
        try {
            mTracker.set(Fields.SCREEN_NAME, getActivity().getString(R.string.GuideDialogFragment));            
            mTracker.send(MapBuilder.createAppView().build());            
		} catch (Exception e) {
		}
    }
}
